package com.douglasalipio.service;

import java.io.IOException;

import com.douglasalipio.model.AutocompleteModel;
import com.google.android.maps.MapView;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AutocompleteAsync extends AsyncTask<String, String[], Boolean> {

	private Context context;
	private AutocompleteModel appMapModel;
	private AutoCompleteTextView component;
	private ArrayAdapter<String> adapter;
	private AutocompleteModel autocompleteModel;
	private boolean error;

	public AutocompleteAsync(Context context, MapView mapView,
			AutoCompleteTextView component, AutocompleteModel autocompleteModel) {

		this.component = component;
		this.autocompleteModel = autocompleteModel;

	}

	@Override
	protected Boolean doInBackground(String... reference) {

		try {
			adapter = new ArrayAdapter<String>(context,
					android.R.layout.simple_dropdown_item_1line,
					appMapModel.getPlaces(reference[0]));

		} catch (IOException e) {

			return true;
		}

		return false;
	}

	@Override
	protected void onPostExecute(Boolean error) {
		super.onPostExecute(error);

		if (error) {

			// tratar erro
		} else {

			component.setAdapter(adapter);
		}
	}

}
